import java.util.LinkedList;
import java.util.List;


public class Box {
	
	private int boxScore=0;
	private List<Card> boxCards= new LinkedList<Card>();
	private int bet;
	private String gameResult="";
	private boolean burned=false;
	private int elevenPointAces=0;
	
	public void setBurned(boolean burned) {
		this.burned = burned;
	}

	private boolean bjCheck=false;
	
	public List<Card> getCards() {
		return boxCards;	
	}
	
	public int getScore(){
		recountScore();
		return boxScore;
		
	}
	public void setBoxCards(Card card){
		boxCards.add(card);
		if (card.getRate() == 'A') {
			elevenPointAces++;
		}
		recountScore();
	}

	private void recountScore() {
		boxScore=0;
		for (int i=0; i<boxCards.size(); i++) {
			boxScore+=boxCards.get(i).getScore();
		}
		if ((boxScore>21)&&(elevenPointAces == 0)) {
			burned=true;
		} if (boxScore>21) {
			do {
				aceCheck();
			} while (elevenPointAces > 0);
		}
	}
	
	private void aceCheck() {
		for (int i=0; i<boxCards.size(); i++) {
			if ((boxCards.get(i).getRate() == 'A')&&(boxCards.get(i).getScore() == 11)) {
				boxCards.get(i).setScore(1);
				break;
			}
		}
	}
	
	public List<Card> getBoxCards(){
		return boxCards;
		
	}
	
	public List<Card> hit(Card card){
		boxCards.add(card);
		recountScore();
		return boxCards;
	}
	
	public int getBet() {
		return bet;
	}

	public void setBet(int bet) {
		this.bet = bet;
	}
	@Override
	public String toString() {
		String res="";
		for (int i=0; i<boxCards.size(); i++) {
		res=res+" "+boxCards.get(i).toString();	
		}
		res+="|"+String.valueOf(getScore());
		return res;
	}

	public boolean isBurned() {
		return burned;
	}

	public String getGameResult() {
		return gameResult;
	}

	public void setGameResult(int dealerScore) {
		recountScore();
		if (burned) {
			gameResult="burned";
		} else {
			if (boxScore>dealerScore) {
				gameResult="win";
			} else {
				if (boxScore<dealerScore) {
					gameResult="lost";
				} else 
					gameResult="stand off";
					
		}
			}
	}

	

}
